using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Specialized;

namespace ru.yourpictures.Core.Imaging.Filters.PhotoFilters
{
    /// <summary>
    /// Summary description for Class1.
    /// Reference: http://www.codeproject.com/KB/cs/yael_image_filters.aspx
    /// </summary>
    public class FloorReflection : FilterBase
    {
        #region TODOs
        // 1. Combination of Box and Floor reflection, using point of view parameter
        //    For the simplicity sake we may assume that image is fixed vertically
        //    i.e. "picture on the wall"
        #endregion

        private float _alphaStartValue = 110;
        private float _alphaDecreaseRate = 4;

        #region Public properties
        /// <summary>
        /// Sets the initial transparecy value of the reflection.
        /// </summary>
        public float AlphaStartValue
        {
          get{ return _alphaStartValue; }
          set{ _alphaStartValue = value; }
        }

        /// <summary>
        /// Sets the decrease rate value of the transparency
        /// </summary>
        public float AlphaDecreaseRate
        {
          get{ return _alphaDecreaseRate; }
          set{ _alphaDecreaseRate = value; }
        }
        #endregion

        #region Ctor
        public FloorReflection(float alphaStartValue, float alphaDecreaseRate)
        {
            _alphaStartValue = alphaStartValue;
            _alphaDecreaseRate = alphaDecreaseRate;
        }
        public FloorReflection() { }
        #endregion

        #region FilterImplementation
        public override void Apply(Bitmap b)
        {
            b = Convert(b);
        }
        /// <summary>
        /// Executes this filter on the input image and returns the result
        /// </summary>
        /// <param name="b">input image</param>
        /// <returns>transformed image</returns>
        /// <example>
        /// <code>
        /// Image transformed;
        /// FloorReflection floorReflection = new FloorReflection();
        /// floorReflection.AlphaDecreaseRate = 3;
        /// transformed = floorReflection.Convert(myImg);
        /// </code>
        /// </example>
        public override Bitmap Convert(Bitmap b)
        {
            _width = b.Width;
            _height = b.Height;

            int reflectionRows = (int)((_alphaStartValue) / _alphaDecreaseRate);
            Bitmap result = new Bitmap(_width, _height + reflectionRows);
            Graphics g = Graphics.FromImage(result);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(b, 0, 0, (float)_width, (float)_height);
            System.Drawing.Color pixelColor, newColor;
            Bitmap raw = b;
            int i, j;
            try
            {
                for (i = 0; i < reflectionRows - 1; i++)
                {
                    for (j = 0; j < _width - 1; j++)
                    {
                        pixelColor = raw.GetPixel(j, _height - i - 1);
                        newColor = System.Drawing.Color.FromArgb((pixelColor.A) * ((int)_alphaStartValue - (i * (int)_alphaDecreaseRate)) / 255, pixelColor.R, pixelColor.G, pixelColor.B);
                        g.DrawRectangle(new Pen(newColor), j, i + _height - 1, 1, 1);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            //for (int i=1 ToolboxBitmapAttribute 
            Pen myTestPen = new Pen(System.Drawing.Color.FromArgb(100, 5, 5, 5));
            //g.FillRectangle(new SolidBrush(Color.FromArgb(100, 5, 5, 5)), 0, 375, 150, 100);

            return result;
        }

        protected override void FilterImplementation()
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
